package com.shop.controller;

import com.shop.constant.Constants;
import com.shop.entity.storeOrderList.StoreGetOrderList;
import com.shop.exception.DAOException;
import com.shop.exception.StoreGoodIsNotExitException;
import com.shop.exception.StoreIsNotExitException;
import com.shop.factory.ObjectFactory;
import com.shop.service.StoreService;
import com.shop.util.GetMap;
import com.shop.util.PrintJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/storeOrder/*")
public class StoreOrderControler  extends HttpServlet {
    StoreService service = (StoreService) ObjectFactory.getInstance(Constants.STORE_SERVICE_KEY);


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        if("/shop/storeOrder/getAllOrderList".equals(uri)) {
            getAllOrderList(req,resp);
        }else if ("/shop/storeOrder/storeUpOrder".equalsIgnoreCase(uri)){
            storeUpOrder(req,resp);
        }else if ("/shop/storeOrder/getAllOrderListLike".equalsIgnoreCase(uri)){
            System.out.println("获取商订房单");
            getAllOrderListLike(req,resp);
        }
    }


    /**
     * int sid 商家id
     * String status 订单详情订单状态，可以没有参数
     *
     */
    private void getAllOrderList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int sid = Integer.parseInt(req.getParameter("s_id"));
        String status = req.getParameter("od_status");
        System.out.println("sid" + sid+ "," +"status" +status);
        int pageNum = Integer.parseInt(req.getParameter("pageNum"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        Map<String, Object> map = null;
        List<StoreGetOrderList> lists = null;
        try {
            lists = service.getAllOrderList(sid,status,pageNum,pageSize);
            map = GetMap.getMap(200, "查看根据订单状态查询订单成功");
            map.put("data",lists);
            PrintJson.printJsonObj(resp,map);
        } catch (StoreIsNotExitException | DAOException e) {
            e.printStackTrace();
            //商家不存在处理异常
            map = GetMap.getMap(441,e.getMessage());
            PrintJson.printJsonObj(resp,map);
            System.out.println("商家不存在");
        }

    }


    private void storeUpOrder(HttpServletRequest req, HttpServletResponse resp) {
        int sid = Integer.parseInt(req.getParameter("s_id"));
        int status = Integer.parseInt(req.getParameter("status"));
        int od_id = Integer.parseInt(req.getParameter("od_id"));
        int gd_id = Integer.parseInt(req.getParameter("gd_id"));
        Map<String, Object> map = null;
        try {
            service.storeUpOrder(sid,status,od_id,gd_id);
            System.out.println("sid" + sid+ "," +"status" +status);
            map = GetMap.getMap(200, "商家修改订单成功");
            PrintJson.printJsonObj(resp,map);
        } catch (StoreIsNotExitException e) {
            e.printStackTrace();
            map = GetMap.getMap(441,e.getMessage());
            PrintJson.printJsonObj(resp,map);
        } catch (StoreGoodIsNotExitException e) {
            e.printStackTrace();
            map = GetMap.getMap(441,e.getMessage());
            PrintJson.printJsonObj(resp,map);
        } catch (DAOException e) {
            e.printStackTrace();
            map = GetMap.getMap(441,e.getMessage());
            PrintJson.printJsonObj(resp,map);
        }

    }

    private void getAllOrderListLike(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("模糊");
        int sid = Integer.parseInt(req.getParameter("s_id"));
        String condition = req.getParameter("condition");
        int pageNum = Integer.parseInt(req.getParameter("pageNum"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        List<StoreGetOrderList> lists = null;
        Map<String, Object> map = null;
        try {
            lists = service.getAllOrderListLikeTName(sid,condition,pageNum,pageSize);
            map = GetMap.getMap(200, "商家模糊查询订单成功");
            map.put("data",lists);
            PrintJson.printJsonObj(resp,map);
        } catch (StoreIsNotExitException e) {
            e.printStackTrace();
            map = GetMap.getMap(441,e.getMessage());
            PrintJson.printJsonObj(resp,map);
        }
    }

}
